package sort;

public class MergeSort {

    public static void process(int[] arr, int left, int right) {
        if (left == right) {
            return;
        }
        int min = left + ((right - left) << 1);
        process(arr, left, min);
        process(arr, min + 1, right);
        merge(arr, left, min, right);
    }

    public static void merge(int[] arr, int left, int min, int right) {
        int[] help = new int[right - left + 1];
        int p1 = left;
        int p2 = min + 1;
        int i = 0;
        while (p1 <= min && p2 <= right) {
            help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
        }
        while (p1 <= min) {
            help[i++] = arr[p1++];
        }
        while (p2 <= right) {
            help[i++] = arr[p2++];
        }
        for (i = 0; i < help.length; i++) {
            arr[left + i] = help[i];
        }
    }
}
